### CODE FOR:
###
### Putting the partisan influence into political context:
### How party attachment and policy preference shape the effect of party cues
###
### PART 2: Stacked barplot figures (i.e., Figures 1 and A1)
###
### VERSION: 2023-MAY-11


rm(list=ls())
gc()


setwd("C:/Users/miros/Desktop/Research/Finland - Party cues in the context of policy preferences")




# LOADING LIBRARIES -------------------------------------------------------
library(haven)
library(dplyr)
library(ggplot2)
library(grid)
library(ggpubr)
library(RColorBrewer)





# DATA --------------------------------------------------------------------

# _ Import ---------------------------------------------------------------
# NOTE: The data file loaded in the next step was prepared in a separate
#       scripts '01_CuesInContext_data-prep.R'. If you haven't run the script, 
#       return to the depository and run that script first. Its output is 
#       'BIBU_long.Rda' loaded in the next step.

load("02-data/BIBU_long.Rda")






# _ Adjustments -----------------------------------------------------------

# Abolishment of the 'Retirement Tube'
BIBU.long$RETIREMENT_TUBE[BIBU.long$RETIREMENT_TUBE == 1] <- "Strongly disagree"
BIBU.long$RETIREMENT_TUBE[BIBU.long$RETIREMENT_TUBE == 2] <- "Disagree"
BIBU.long$RETIREMENT_TUBE[BIBU.long$RETIREMENT_TUBE == 3] <- "Agree"
BIBU.long$RETIREMENT_TUBE[BIBU.long$RETIREMENT_TUBE == 4] <- "Strongly agree"

BIBU.long$RETIREMENT_TUBE <- factor(BIBU.long$RETIREMENT_TUBE, 
                                    levels = c("Strongly agree",
                                               "Agree",
                                               "Disagree",
                                               "Strongly disagree"))


# Extension of the compulsory schooling
BIBU.long$COMPULSORY_SCHOOLING[BIBU.long$COMPULSORY_SCHOOLING == 1] <- "Strongly disagree"
BIBU.long$COMPULSORY_SCHOOLING[BIBU.long$COMPULSORY_SCHOOLING == 2] <- "Disagree"
BIBU.long$COMPULSORY_SCHOOLING[BIBU.long$COMPULSORY_SCHOOLING == 3] <- "Agree"
BIBU.long$COMPULSORY_SCHOOLING[BIBU.long$COMPULSORY_SCHOOLING == 4] <- "Strongly agree"

BIBU.long$COMPULSORY_SCHOOLING <- factor(BIBU.long$COMPULSORY_SCHOOLING, 
                                         levels = c("Strongly agree",
                                                    "Agree",
                                                    "Disagree",
                                                    "Strongly disagree"))




# FIGURE 1 ----------------------------------------------------------------

# NOTE: The following section generates Figure 1 - i.e., a stacked bar plot
#       showing the distribution of individual responses about policy 
#       preferences among government vs opposition voters.


# _ Generating data.frame with group totals -------------------------------

barplot.data <-
  rbind(
    data.frame(
      filter(BIBU.long, ROUND == "Round 1" & 
               !is.na(GovParty.Overall) & 
               !is.na(RETIREMENT_TUBE)) %>%
        group_by(Group = GovParty.Overall) %>%
        count(Answer = RETIREMENT_TUBE) %>%
        mutate(Group_total = sum(n)) %>%
        mutate(Share = round((n/Group_total)*100, 1)) %>%
        mutate(Reform = c("Retirement Tube"))),
    
    data.frame(
      filter(BIBU.long, ROUND == "Round 1" & 
               !is.na(GovParty.Overall) & 
               !is.na(COMPULSORY_SCHOOLING)) %>%
        group_by(Group = GovParty.Overall) %>%
        count(Answer = COMPULSORY_SCHOOLING) %>%
        mutate(Group_total = sum(n)) %>%
        mutate(Share = round((n/Group_total)*100, 1)) %>%
        mutate(Reform = c("Compulsory Schooling")))
  )





# _ Figure ----------------------------------------------------------------

ggarrange(
  
  # Abolishment of the 'Retirement Tube'
  ggplot(filter(barplot.data, Reform == "Retirement Tube"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = c("#377EB8","#377EB8", "#377EB8","#377EB8", "#E41A1C","#E41A1C","#E41A1C", "#E41A1C"),
             alpha = c(0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2.75, position = position_stack(vjust = 0.55), alpha = 0.85, lineheight = 0.85) +
    
    # Header
    geom_rect(xmin = 0.5, xmax = 2.5, ymin = 102.5, ymax = 115, colour = "black", fill = "grey90") +
    annotate("text", x = 1.5, y = 108.75, hjust = 0.5, vjust = -0.15, label = "Unpopular policy reform", size = 12/.pt, fontface = "bold") +
    annotate("text", x = 1.5, y = 108.75, hjust = 0.5, vjust =  1.45, label = "Abolition of the 'retirement tube'", size = 8.5/.pt) +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("Government voters", "Opposition voters"),
                     labels = c("Government\nvoters", "Opposition\nvoters")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(xlim = c(0.5, 2.5), ylim = c(0, 115), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.x = element_text(size = 10),
          panel.grid.major.x = element_blank(), 
          panel.grid.major.y = element_line(colour = "gray90"), 
          panel.grid.minor.y = element_line(colour = "gray90"), 
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.02, 0.2, 0.2),"cm")),
  
  
  # Extension of the compulsory schooling age
  ggplot(filter(barplot.data, Reform == "Compulsory Schooling"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = c("#377EB8","#377EB8", "#377EB8","#377EB8", "#E41A1C","#E41A1C","#E41A1C", "#E41A1C"),
             alpha = c(0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2.75, position = position_stack(vjust = 0.5), alpha = 0.85, lineheight = 0.85) +
    
    # Header
    geom_rect(xmin = 0.5, xmax = 2.5, ymin = 102.5, ymax = 115, colour = "black", fill = "grey90") +
    annotate("text", x = 1.5, y = 108.75, hjust = 0.5, vjust = -0.15, label = "Popular policy reform", size = 12/.pt, fontface = "bold") +
    annotate("text", x = 1.5, y = 108.75, hjust = 0.5, vjust =  1.45, label = "Extension of the compulsory schooling age", size = 8.5/.pt) +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("Government voters", "Opposition voters"),
                     labels = c("Government\nvoters", "Opposition\nvoters")) +
    labs(x = NULL, y = "Share") +
    coord_cartesian(xlim = c(0.5, 2.5), ylim = c(0, 115), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.x = element_text(size = 10),
          axis.text.y = element_blank(),
          axis.title.y = element_blank(),
          panel.grid.major.x = element_blank(), 
          panel.grid.major.y = element_line(colour = "gray90"), 
          panel.grid.minor.y = element_line(colour = "gray90"), 
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.2, 0.2, 0.02),"cm")),
  
  nrow = 1, align = "hv")



# Saving the output
ggsave(filename = "04-figures and models/Fig 1 - reform popularity.png", width = 20, height = 12, units = "cm", dpi = 600)

rm(barplot.data)






# FIGURE 2: Alluvial diagram ----------------------------------------------

# _ Generating data objects -----------------------------------------------

# First, recoding the dependent variable into a binary version
# Support for abolishment of the 'Retirement Tube'
BIBU.long$RETIREMENT_TUBE_BI <- NA
BIBU.long$RETIREMENT_TUBE_BI[BIBU.long$RETIREMENT_TUBE == "Strongly agree" | BIBU.long$RETIREMENT_TUBE == "Agree"] <- 1
BIBU.long$RETIREMENT_TUBE_BI[BIBU.long$RETIREMENT_TUBE == "Strongly disagree" | BIBU.long$RETIREMENT_TUBE == "Disagree"] <- 0

# Support for extension of compulsory schooling
BIBU.long$COMPULSORY_SCHOOLING_BI <- NA
BIBU.long$COMPULSORY_SCHOOLING_BI[BIBU.long$COMPULSORY_SCHOOLING == "Strongly agree" | BIBU.long$COMPULSORY_SCHOOLING == "Agree"] <- 1
BIBU.long$COMPULSORY_SCHOOLING_BI[BIBU.long$COMPULSORY_SCHOOLING == "Strongly disagree" | BIBU.long$COMPULSORY_SCHOOLING == "Disagree"] <- 0



# Second step--including only the observations participating in both waves
# Creating a subset of those who participated in both waves and provided all information
BIBU.long.subset.TUBE <- na.omit(BIBU.long[c("RETIREMENT_TUBE_BI", "MEMBERID", "ROUND", "GovParty.Overall")])
BIBU.long.subset.SCHOOLING <- na.omit(BIBU.long[c("COMPULSORY_SCHOOLING_BI", "MEMBERID", "ROUND", "GovParty.Overall")])

# Finding out which 'MEMBERID' is included exactly twice in the data
BIBU.long.subset.TUBE.twice <- data.frame(table(BIBU.long.subset.TUBE$MEMBERID))
BIBU.long.subset.TUBE.twice$Include <- ifelse(BIBU.long.subset.TUBE.twice$Freq == 2, 1, 0)

BIBU.long.subset.SCHOOLING.twice <- data.frame(table(BIBU.long.subset.SCHOOLING$MEMBERID))
BIBU.long.subset.SCHOOLING.twice$Include <- ifelse(BIBU.long.subset.SCHOOLING.twice$Freq == 2, 1, 0)

# Attaching the 'Include' variable (identifying respondents participating in both waves)
BIBU.long.subset.TUBE <- merge(BIBU.long.subset.TUBE, BIBU.long.subset.TUBE.twice[c("Var1", "Include")], by.x = "MEMBERID", by.y = "Var1")
BIBU.long.subset.SCHOOLING <- merge(BIBU.long.subset.SCHOOLING, BIBU.long.subset.SCHOOLING.twice[c("Var1", "Include")], by.x = "MEMBERID", by.y = "Var1")

# Subsetting only relevant observations (i.e., people participating in both waves)
BIBU.long.subset.TUBE <- subset(BIBU.long.subset.TUBE, Include == 1)
BIBU.long.subset.SCHOOLING <- subset(BIBU.long.subset.SCHOOLING, Include == 1)

# Removing irrelevant objects and variables
BIBU.long.subset.TUBE$Include <- NULL
BIBU.long.subset.SCHOOLING$Include <- NULL

# Interim cleanup
rm(BIBU.long.subset.TUBE.twice, BIBU.long.subset.SCHOOLING.twice)

# NOTE: Now, the 'BIBU.long.subset.TUBE' and 'BIBU.long.subset.SCHOOLING' include only 
#       respondents participating in both waves and ready for fixed-model estimation.





# _ Calculating relevant statistics ---------------------------------------

# Saving the total numbers of observations (for easier code)
N.gov.TUBE <- nrow(subset(BIBU.long.subset.TUBE, GovParty.Overall == "Government voters"))
N.opp.TUBE <- nrow(subset(BIBU.long.subset.TUBE, GovParty.Overall == "Opposition voters"))

N.gov.SCHOOL <- nrow(subset(BIBU.long.subset.SCHOOLING, GovParty.Overall == "Government voters"))
N.opp.SCHOOL <- nrow(subset(BIBU.long.subset.SCHOOLING, GovParty.Overall == "Opposition voters"))


# Creating labels
BIBU.long.subset.TUBE$Response <- 
  ifelse(BIBU.long.subset.TUBE$RETIREMENT_TUBE_BI == 1, 
         "Strongly agree\nand agree", 
         "Strongly disagree\nand disagree")

BIBU.long.subset.SCHOOLING$Response <- 
  ifelse(BIBU.long.subset.SCHOOLING$COMPULSORY_SCHOOLING_BI == 1, 
         "Strongly agree\nand agree", 
         "Strongly disagree\nand disagree")


# Setting factor levels
BIBU.long.subset.TUBE$RETIREMENT_TUBE_BI <- factor(BIBU.long.subset.TUBE$RETIREMENT_TUBE_BI, levels = c(1, 0))
BIBU.long.subset.SCHOOLING$COMPULSORY_SCHOOLING_BI <- factor(BIBU.long.subset.SCHOOLING$COMPULSORY_SCHOOLING_BI, levels = c(1, 0))


# Adding relevant variables to the data
BIBU.long.subset.TUBE <- 
  BIBU.long.subset.TUBE %>% 
  add_count(RETIREMENT_TUBE_BI, GovParty.Overall, ROUND) %>%
  add_count(GovParty.Overall, ROUND) %>%
  rename(response_total = n,
         group_total = nn) %>%
  mutate(share = response_total / group_total) %>%
  mutate(label = paste0(round(share*100, 1), "% (N=", response_total, ")"))

BIBU.long.subset.SCHOOLING <- 
  BIBU.long.subset.SCHOOLING %>% 
  add_count(COMPULSORY_SCHOOLING_BI, GovParty.Overall, ROUND) %>%
  add_count(GovParty.Overall, ROUND) %>%
  rename(response_total = n,
         group_total = nn) %>%
  mutate(share = response_total / group_total) %>%
  mutate(label = paste0(round(share*100, 1), "% (N=", response_total, ")"))




# _ Figure ----------------------------------------------------------------


# __ Unpopular policy -----------------------------------------------------

w.retrenchment <-
  ggarrange(
    # Government parties
    ggplot(subset(BIBU.long.subset.TUBE, GovParty.Overall == "Government voters"), 
           aes(x = ROUND, stratum = RETIREMENT_TUBE_BI, fill = as.factor(RETIREMENT_TUBE_BI), color = as.factor(RETIREMENT_TUBE_BI), alluvium = MEMBERID)) +
      geom_hline(yintercept = c(N.gov.TUBE/2*seq(0, 1, 0.1)), color = "grey90") +
      scale_fill_manual(values = brewer.pal(n = 12, name = "Paired")[c(2, 6)]) +
      scale_color_manual(values = brewer.pal(n = 12, name = "Paired")[c(2, 6)]) +
      geom_flow(width = 0.5) +
      geom_stratum(alpha = 0.5, width = 0.5) +
      scale_x_discrete(limits = c("Round 1", "Round 2"), labels = c("Before\nthe reform\n(September 2020)", "After\nthe reform\n(February 2021)")) +
      labs(title = NULL, x = NULL, y = NULL) +
      scale_y_continuous(breaks = c(N.gov.TUBE/2*seq(0, 1, 0.2)), labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
      geom_rect(ymin = N.gov.TUBE/2, ymax = N.gov.TUBE/2*1.2, xmin = 0.75, xmax = 2.25, colour = "black", fill = "grey90") +
      annotate("text", x = 1.5, y = N.gov.TUBE/2*1.1, hjust = 0.5, vjust = 0.5, label = "Government voters", fontface = 2, size = 12/.pt) +
      coord_cartesian(xlim = c(0.75, 2.25), ylim = c(0, N.gov.TUBE/2*1.15), expand = FALSE, clip = "off") +
      geom_text(aes(label = paste0(Response, "\n", label)), stat = "stratum", size = 3, color = "black", alpha = 0.85, lineheight = 0.85) +
      geom_segment(x = 0.75, xend = 0.75, y = 0, yend = N.gov.TUBE/2, color = "black") +
      geom_segment(x = 0.75, xend = 2.25, y = 0, yend = 0, color = "black") +
      geom_segment(x = 2.25, xend = 2.25, y = 0, yend = N.gov.TUBE/2, color = "black") +
      theme(panel.grid = element_blank(), 
            panel.border = element_blank(),
            axis.text.x = element_blank(),
            axis.ticks.x = element_blank(),
            strip.background = element_blank(), 
            panel.background = element_blank(),
            legend.position = "none", 
            axis.text.y.right = element_blank(),
            plot.margin = unit(c(0.5, 0.15, 0.15, 0.25),"cm")),
    
    # Opposition parties
    ggplot(subset(BIBU.long.subset.TUBE, GovParty.Overall == "Opposition voters"), 
           aes(x = ROUND, stratum = RETIREMENT_TUBE_BI, fill = as.factor(RETIREMENT_TUBE_BI), color = as.factor(RETIREMENT_TUBE_BI), alluvium = MEMBERID)) +
      geom_hline(yintercept = c(N.opp.TUBE/2*seq(0, 1, 0.1)), color = "grey90") +
      scale_fill_manual(values = brewer.pal(n = 12, name = "Paired")[c(1, 5)]) +
      scale_color_manual(values = brewer.pal(n = 12, name = "Paired")[c(1, 5)]) +
      geom_flow(width = 0.5) +
      geom_stratum(alpha = 0.5, width = 0.5) +
      scale_x_discrete(limits = c("Round 1", "Round 2"), labels = c("Before\nthe reform\n(September 2020)", "After\nthe reform\n(February 2021)")) +
      labs(title = NULL, x = NULL, y = NULL) +
      scale_y_continuous(breaks = c(N.opp.TUBE/2*seq(0, 1, 0.2)), labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
      geom_rect(ymin = N.opp.TUBE/2, ymax = N.opp.TUBE/2*1.2, xmin = 0.75, xmax = 2.25, colour = "black", fill = "grey90") +
      annotate("text", x = 1.5, y = N.opp.TUBE/2*1.1, hjust = 0.5, vjust = 0.5, label = "Opposition voters", fontface = 2, size = 12/.pt) +
      geom_rect(ymin = 0, ymax = N.opp.TUBE/2, xmin = 2.25, xmax = 2.5, colour = "black", fill = "grey90") +
      
      annotate("text", x = 2.375, y = N.opp.TUBE/2*0.5, hjust = 0.5, vjust = -0.4, label = "Unpopular policy reform", fontface = 2, angle = 270, size = 12/.pt) +
      annotate("text", x = 2.375, y = N.opp.TUBE/2*0.5, hjust = 0.5, vjust =  1.4, label = "Abolition of the 'retirement tube'", angle = 270, size = 9/.pt) +
      
      coord_cartesian(xlim = c(0.75, 2.5), ylim = c(0, N.opp.TUBE/2*1.15), expand = FALSE, clip = "off") +
      geom_text(aes(label = paste0(Response, "\n", label)), stat = "stratum", size = 3, color = "black", alpha = 0.85, lineheight = 0.85) +
      geom_segment(x = 0.75, xend = 0.75, y = 0, yend = N.opp.TUBE/2, color = "black") +
      geom_segment(x = 0.75, xend = 2.5, y = 0, yend = 0, color = "black") +
      theme(panel.grid = element_blank(), 
            axis.text = element_blank(),
            axis.ticks = element_blank(),
            panel.border = element_blank(), 
            strip.background = element_blank(), 
            panel.background = element_blank(),
            legend.position = "none", 
            axis.text.y.right = element_blank(),
            plot.margin = unit(c(0.5, 0.25, 0.15, 0.15),"cm")),
    
    # Specs
    ncol = 2, widths = c(9.55, 10))



# _ Popular policy --------------------------------------------------------

w.expansion <-
  ggarrange(
    # Government parties
    ggplot(subset(BIBU.long.subset.SCHOOLING, GovParty.Overall == "Government voters"), 
           aes(x = ROUND, stratum = COMPULSORY_SCHOOLING_BI, fill = as.factor(COMPULSORY_SCHOOLING_BI), color = as.factor(COMPULSORY_SCHOOLING_BI), alluvium = MEMBERID)) +
      geom_hline(yintercept = c(N.gov.SCHOOL/2*seq(0, 1, 0.1)), color = "grey90") +
      scale_fill_manual(values = brewer.pal(n = 12, name = "Paired")[c(2, 6)]) +
      scale_color_manual(values = brewer.pal(n = 12, name = "Paired")[c(2, 6)]) +
      geom_flow(width = 0.5) +
      geom_stratum(alpha = 0.5, width = 0.5) +
      scale_x_discrete(limits = c("Round 1", "Round 2"), labels = c("Before\nthe reform\n(September 2020)", "After\nthe reform\n(February 2021)")) +
      labs(title = NULL, x = NULL, y = NULL) +
      scale_y_continuous(breaks = c(N.gov.SCHOOL/2*seq(0, 1, 0.2)), labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
      coord_cartesian(xlim = c(0.75, 2.25), ylim = c(0, N.gov.SCHOOL/2), expand = FALSE, clip = "off") +
      geom_text(aes(label = paste0(Response, "\n", label)), stat = "stratum", size = 3, color = "black", alpha = 0.85, lineheight = 0.85) +
      geom_segment(x = 0.75, xend = 0.75, y = 0, yend = N.gov.SCHOOL/2, color = "black") +
      geom_segment(x = 0.75, xend = 2.25, y = 0, yend = 0, color = "black") +
      geom_segment(x = 2.25, xend = 2.25, y = 0, yend = N.gov.SCHOOL/2, color = "black") +
      geom_segment(x = 0.75, xend = 2.25, y = N.gov.SCHOOL/2, yend = N.gov.SCHOOL/2, color = "black") +
      theme(panel.grid = element_blank(), 
            panel.border = element_blank(), 
            axis.ticks.x = element_blank(),
            strip.background = element_blank(), 
            panel.background = element_blank(),
            legend.position = "none", 
            axis.text.y.right = element_blank(),
            plot.margin = unit(c(0.15, 0.15, 0.15, 0.25),"cm")),

    
    # Opposition parties
    ggplot(subset(BIBU.long.subset.SCHOOLING, GovParty.Overall == "Opposition voters"), 
           aes(x = ROUND, stratum = COMPULSORY_SCHOOLING_BI, fill = as.factor(COMPULSORY_SCHOOLING_BI), color = as.factor(COMPULSORY_SCHOOLING_BI), alluvium = MEMBERID)) +
      geom_hline(yintercept = c(N.opp.SCHOOL/2*seq(0, 1, 0.1)), color = "grey90") +
      scale_fill_manual(values = brewer.pal(n = 12, name = "Paired")[c(1, 5)]) +
      scale_color_manual(values = brewer.pal(n = 12, name = "Paired")[c(1, 5)]) +
      geom_flow(width = 0.5) +
      geom_stratum(alpha = 0.5, width = 0.5) +
      scale_x_discrete(limits = c("Round 1", "Round 2"), labels = c("Before\nthe reform\n(September 2020)", "After\nthe reform\n(February 2021)")) +
      labs(title = NULL, x = NULL, y = NULL) +
      scale_y_continuous(breaks = c(N.opp.SCHOOL/2*seq(0, 1, 0.2)), labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
      geom_rect(ymin = 0, ymax = N.opp.SCHOOL/2, xmin = 2.25, xmax = 2.5, colour = "black", fill = "grey90") +
      annotate("text", x = 2.375, y = N.opp.SCHOOL/2*0.5, hjust = 0.5, vjust = -0.4, label = "Popular policy reform", fontface = 2, angle = 270, size = 12/.pt) +
      annotate("text", x = 2.375, y = N.opp.SCHOOL/2*0.5, hjust = 0.5, vjust =  1.4, label = "Extension of the compulsory school age", angle = 270, size = 9/.pt) +
      coord_cartesian(xlim = c(0.75, 2.5), ylim = c(0, N.opp.SCHOOL/2), expand = FALSE, clip = "off") +
      geom_text(aes(label = paste0(Response, "\n", label)), stat = "stratum", size = 3, color = "black", alpha = 0.85, lineheight = 0.85) +
      geom_segment(x = 0.75, xend = 0.75, y = 0, yend = N.opp.SCHOOL/2, color = "black") +
      geom_segment(x = 0.75, xend = 2.5, y = 0, yend = 0, color = "black") +
      geom_segment(x = 0.75, xend = 2.5, y = N.opp.SCHOOL/2, yend = N.opp.SCHOOL/2, color = "black") +
      theme(panel.grid = element_blank(), 
            axis.text.y = element_blank(),
            axis.ticks.x = element_blank(),
            axis.ticks.y = element_blank(),
            panel.border = element_blank(), 
            strip.background = element_blank(), 
            panel.background = element_blank(),
            legend.position = "none", 
            axis.text.y.right = element_blank(),
            plot.margin = unit(c(0.15, 0.25, 0.15, 0.15),"cm")),
    
    # Specs
    ncol = 2, widths = c(9.55, 10))


# Putting it all together
ggarrange(w.retrenchment, w.expansion,
          nrow = 2, heights = c(10, 9.85))

# Saving the output
ggsave(filename = "04-figures and models/Fig 2 - alluvial changes.png", width = 25, height = 16, units = "cm", dpi = 600)






# _ Calculating sizes of the groups that changed opinion ------------------
# NOTE: These statistics are used in the text

# 'Retirement Tube'
BIBU.long.change.TUBE <-
  merge(
    subset(BIBU.long.subset.TUBE[c("MEMBERID", "RETIREMENT_TUBE_BI", "ROUND", "GovParty.Overall", "Response")], ROUND == "Round 1"),
    subset(BIBU.long.subset.TUBE[c("MEMBERID", "RETIREMENT_TUBE_BI", "ROUND", "GovParty.Overall", "Response")], ROUND == "Round 2"),
    by = "MEMBERID"
  )

BIBU.long.change.TUBE$Change <- NA
BIBU.long.change.TUBE$Change[BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.x == 1 & BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.y == 1] <- "Agree-agree"
BIBU.long.change.TUBE$Change[BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.x == 1 & BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.y == 0] <- "Agree-disagree"
BIBU.long.change.TUBE$Change[BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.x == 0 & BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.y == 1] <- "Disagree-agree"
BIBU.long.change.TUBE$Change[BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.x == 0 & BIBU.long.change.TUBE$RETIREMENT_TUBE_BI.y == 0] <- "Disagree-disagree"

table(BIBU.long.change.TUBE$Change, BIBU.long.change.TUBE$GovParty.Overall.x, useNA = "ifany")


# 'Retirement Tube'
BIBU.long.change.SCHOOLING <-
  merge(
    subset(BIBU.long.subset.SCHOOLING[c("MEMBERID", "COMPULSORY_SCHOOLING_BI", "ROUND", "GovParty.Overall", "Response")], ROUND == "Round 1"),
    subset(BIBU.long.subset.SCHOOLING[c("MEMBERID", "COMPULSORY_SCHOOLING_BI", "ROUND", "GovParty.Overall", "Response")], ROUND == "Round 2"),
    by = "MEMBERID"
  )

BIBU.long.change.SCHOOLING$Change <- NA
BIBU.long.change.SCHOOLING$Change[BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.x == 1 & BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.y == 1] <- "Agree-agree"
BIBU.long.change.SCHOOLING$Change[BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.x == 1 & BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.y == 0] <- "Agree-disagree"
BIBU.long.change.SCHOOLING$Change[BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.x == 0 & BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.y == 1] <- "Disagree-agree"
BIBU.long.change.SCHOOLING$Change[BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.x == 0 & BIBU.long.change.SCHOOLING$COMPULSORY_SCHOOLING_BI.y == 0] <- "Disagree-disagree"

table(BIBU.long.change.SCHOOLING$Change, BIBU.long.change.SCHOOLING$GovParty.Overall.x, useNA = "ifany")


# removing irrelevant objects
rm(w.retrenchment, w.expansion,
   N.gov.SCHOOL, N.gov.TUBE, N.opp.SCHOOL, N.opp.TUBE,
   BIBU.long.subset.TUBE, BIBU.long.subset.SCHOOLING,
   BIBU.long.change.TUBE, BIBU.long.change.SCHOOLING)













# _________________ -------------------------------------------------------
# ONLINE APPENDIX ---------------------------------------------------------


# FIGURE A1 ---------------------------------------------------------------

# NOTE: The following section generates Figure 1 - i.e., a stacked bar plot
#       showing the distribution of individual responses about policy 
#       preferences among electorates of individual parties.



# _ Generating data.frame with group totals -------------------------------

barplot.data <-
  rbind(
    data.frame(
      filter(BIBU.long, ROUND == "Round 1" & 
               !is.na(PartyChoice.Overall) & 
               !is.na(RETIREMENT_TUBE)) %>%
        group_by(Group = PartyChoice.Overall) %>%
        count(Answer = RETIREMENT_TUBE, .drop = FALSE) %>%
        mutate(Group_total = sum(n)) %>%
        mutate(Share = round((n/Group_total)*100, 1)) %>%
        mutate(Reform = c("Retirement Tube"))),
    
    data.frame(
      filter(BIBU.long, ROUND == "Round 1" & 
               !is.na(PartyChoice.Overall) & 
               !is.na(COMPULSORY_SCHOOLING)) %>%
        group_by(Group = PartyChoice.Overall) %>%
        count(Answer = COMPULSORY_SCHOOLING, .drop = FALSE) %>%
        mutate(Group_total = sum(n)) %>%
        mutate(Share = round((n/Group_total)*100, 1)) %>%
        mutate(Reform = c("Compulsory Schooling")))
  )


# Excluding non-voters
barplot.data <- filter(barplot.data, Group != "Non-voter")


# Adding information about government vs opposition per party
barplot.data <- 
  BIBU.long %>%
  select(Government = GovParty.Overall, 
         Group = PartyChoice.Overall) %>%
  unique() %>%
  right_join(barplot.data)


# Eliminating zeros from one line (so it won't appear as a label)
barplot.data[53, 3:6] <- NA




# _ Figure ----------------------------------------------------------------

ggarrange(
  # (Upper left) Government voters: Abolishment of the 'Retirement Tube'
  ggplot(filter(barplot.data, Reform == "Retirement Tube" & Government == "Government voters"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Grid
    geom_hline(yintercept = seq(0, 100, 10), color = "gray90") +
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = "#E41A1C",
             alpha = c(0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2, position = position_stack(vjust = 0.55), alpha = 0.85, lineheight = 0.85) +
    
    # Header
    geom_rect(xmin = 0.35, xmax = 5.65, ymin = 102.5, ymax = 120, colour = "black", fill = "grey90") +
    annotate("text", x = 3, y = 111.25, hjust = 0.5, vjust = 0.5, label = "Government voters", size = 12/.pt, fontface = "bold") +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("Social Democrats", "Left Alliance", "Greens", "Centre Party", "Swedish Peoples Party"),
                     labels = c("Social\nDemocrats", "Left\nAlliance", "Greens", "Centre\nParty", "Swedish Peoples\nParty")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(xlim = c(0.35, 5.65), ylim = c(0, 120), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.x = element_blank(),
          panel.grid = element_blank(),
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.02, 0.2, 0.2),"cm")),
  
  
  
  # (Upper right) Opposition voters: Abolishment of the 'Retirement Tube'
  ggplot(filter(barplot.data, Reform == "Retirement Tube" & Government == "Opposition voters"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Grid
    geom_hline(yintercept = seq(0, 100, 10), color = "gray90") +
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = "#377EB8",
             alpha = c(0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2, position = position_stack(vjust = 0.55), alpha = 0.85, lineheight = 0.85) +
    
    # Header
    geom_rect(xmin = 0.35, xmax = 5.65, ymin = 102.5, ymax = 120, colour = "black", fill = "grey90") +
    annotate("text", x = 3, y = 111.25, hjust = 0.5, vjust = 0.5, label = "Opposition voters", size = 12/.pt, fontface = "bold") +
    
    # Header
    geom_rect(xmin = 5.65, xmax = 6.4, ymin = 0, ymax = 102.5, colour = "black", fill = "grey90") +
    annotate("text", x = 6.025, y = 50, angle = 270, hjust = 0.5, vjust = -0.15, label = "Unpopular policy reform", size = 12/.pt, fontface = "bold") +
    annotate("text", x = 6.025, y = 50, angle = 270, hjust = 0.5, vjust =  1.45, label = "Abolition of the 'retirement tube'", size = 8.5/.pt) +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("National Coalition", "Christian Democrats", "True Finns", "Movement Now", "Other"),
                     labels = c("National\nCoalition", "Christian\nDemocrats", "True\nFinns", "Movement\nNow", "Other")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(xlim = c(0.35, 6.4), ylim = c(0, 120), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.y = element_blank(),
          axis.text.x = element_blank(),
          panel.grid = element_blank(),
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.2, 0.2, 0.02),"cm")),
  
  
  
  # (Lower left) Government voters: Extension of the compulsory schooling age
  ggplot(filter(barplot.data, Reform == "Compulsory Schooling" & Government == "Government voters"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Grid
    geom_hline(yintercept = seq(0, 100, 10), color = "gray90") +
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = "#E41A1C",
             alpha = c(0.2, 0.35, 0.55, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2, position = position_stack(vjust = 0.55), alpha = 0.85, lineheight = 0.85) +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("Social Democrats", "Left Alliance", "Greens", "Centre Party", "Swedish Peoples Party"),
                     labels = c("Social\nDemocrats", "Left\nAlliance", "Greens", "Centre\nParty", "Swedish Peoples\nParty")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(xlim = c(0.35, 5.65), ylim = c(0, 100), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.x = element_text(size = 10),
          panel.grid = element_blank(),
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.02, 0.2, 0.2),"cm")),
  
  
  
  # (Lower right) Opposition voters: Extension of the compulsory schooling age
  ggplot(filter(barplot.data, Reform == "Compulsory Schooling" & Government == "Opposition voters"), 
         aes(x = rev(Group), y = rev(Share))) + 
    
    # Grid
    geom_hline(yintercept = seq(0, 100, 10), color = "gray90") +
    
    # Bars
    geom_bar(stat = "identity",
             color = "black",
             fill = "#377EB8",
             alpha = c(0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7, 0.2, 0.35, 0.55, 0.7)) +
    geom_text(aes(label = paste0(rev(Answer), "\n(", rev(Share), "%, N=", rev(n), ")")), 
              color = "black", size = 2, position = position_stack(vjust = 0.55), alpha = 0.85, lineheight = 0.85) +
    
    # Header
    geom_rect(xmin = 5.65, xmax = 6.4, ymin = 0, ymax = 102.5, colour = "black", fill = "grey90") +
    annotate("text", x = 6.025, y = 50, angle = 270, hjust = 0.5, vjust = -0.15, label = "Popular policy reform", size = 12/.pt, fontface = "bold") +
    annotate("text", x = 6.025, y = 50, angle = 270, hjust = 0.5, vjust =  1.45, label = "Extension of the compulsory schooling age", size = 8.5/.pt) +
    
    # Specs
    scale_y_continuous(breaks = seq(0, 100, 20),
                       labels = c("0%", "20%", "40%", "60%", "80%", "100%")) +
    scale_x_discrete(limits = c("National Coalition", "Christian Democrats", "True Finns", "Movement Now", "Other"),
                     labels = c("National\nCoalition", "Christian\nDemocrats", "True\nFinns", "Movement\nNow", "Other")) +
    labs(x = NULL, y = NULL) +
    coord_cartesian(xlim = c(0.35, 6.4), ylim = c(0, 100), expand = FALSE, clip = "off") +
    theme(text = element_text(color = "black"),
          axis.text.y = element_blank(),
          axis.text.x = element_text(size = 10),
          panel.grid = element_blank(),
          panel.background = element_rect(fill = "white"),
          plot.background = element_rect(fill = "white"),
          panel.border = element_rect(fill = "transparent", color = "transparent"),
          axis.ticks = element_blank(),
          plot.margin = unit(c(0.2, 0.2, 0.2, 0.02),"cm")),
  
  
  
  # Specs
  nrow = 2, heights = c(10.6, 10),
  ncol = 2, widths = c(10, 10.6))



# Saving the output
ggsave(filename = "04-figures and models/Fig A1 - reform popularity per parties.png", width = 25, height = 17.5, units = "cm", dpi = 600)


# Cleanup 
rm(barplot.data, BIBU.long)





# FIGURE A2 --------------------------------------------------------------


# _ Import: Loading the raw (wide) data file -----------------------------
# NOTE: The data file loaded in the next step was prepared in a separate
#       scripts '01_CuesInContext_data-prep.R'. If you haven't run the script, 
#       return to the depository and run that script first. Its output is 
#       'BIBU_panel.Rda' loaded in the next step.

load("02-data/BIBU_panel.Rda")



# _ Histograms: Opinion differences for individual policy issues ---------

annotate_figure(
  ggarrange(
    # Abolishment of the "Retirement Tube"
    ggplot(BIBU.panel, aes(x = RETIREMENT_TUBE_DIFF)) +
      geom_histogram(binwidth = 1, alpha = 0.4, colour = "black") +
      labs(x = NULL, y = NULL) +
      scale_x_continuous(breaks = c(-3:3), 
                         labels = c("-3\nFull switch to\ndisagreement", "-2", "-1", "0\nNo change", "+1", "+2", "+3\nFull switch to\nagreement")) + 
      scale_y_continuous(breaks = seq(from = 0, to = 1000, by = 200)) +
      coord_cartesian(xlim = c(-3.75, 3.75), ylim = c(0, 1150), expand = FALSE, clip = "off") +
      geom_rect(xmin = -3.75, xmax = 3.75, ymin = 1000, ymax = 1150, colour = "black", fill = "grey85") +
      annotate("text", x = 0, y = 1075, hjust = 0.5, vjust = -0.30, label = "Unpopular policy reform", fontface = 2, size = 11/.pt) +
      annotate("text", x = 0, y = 1075, hjust = 0.5, vjust =  1.60, label = "Abolition of the 'retirement tube'", size = 9/.pt) +
      theme(panel.border = element_rect(fill = "transparent"), panel.background = element_rect(colour = "transparent", fill = "transparent"),
            plot.background = element_rect(colour = "transparent"),
            panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),
            panel.grid.major.y = element_line(colour = "gray90"), panel.grid.minor.y = element_line(colour = "gray90"),
            plot.margin=unit(c(0.5, 0.5, 0.5, 0.5),"cm")),
    
    # Increase of age limit for compulsory schooling
    ggplot(BIBU.panel, aes(x = COMPULSORY_SCHOOLING_DIFF)) +
      geom_histogram(binwidth = 1, alpha = 0.4, colour = "black") +
      labs(x = NULL, y = NULL) +
      scale_x_continuous(breaks = c(-3:3), 
                         labels = c("-3\nFull switch to\ndisagreement", "-2", "-1", "0\nNo change", "+1", "+2", "+3\nFull switch to\nagreement")) + 
      scale_y_continuous(breaks = seq(from = 0, to = 1000, by = 200)) +
      coord_cartesian(xlim = c(-3.75, 3.75), ylim = c(0, 1150), expand = FALSE, clip = "off") +
      geom_rect(xmin = -3.75, xmax = 3.75, ymin = 1000, ymax = 1150, colour = "black", fill = "grey85") +
      annotate("text", x = 0, y = 1075, hjust = 0.5, vjust = -0.30, label = "Popular policy reform", fontface = 2, size = 11/.pt) +
      annotate("text", x = 0, y = 1075, hjust = 0.5, vjust =  1.60, label = "Extension of the compulsory school age", size = 9/.pt) +
      theme(panel.border = element_rect(fill = "transparent"), panel.background = element_rect(colour = "transparent", fill = "transparent"),
            plot.background = element_rect(colour = "transparent"),
            panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(),
            panel.grid.major.y = element_line(colour = "gray90"), panel.grid.minor.y = element_line(colour = "gray90"),
            plot.margin=unit(c(0.5, 0.5, 0.5, 0.5),"cm")),
    
    ncol = 2, nrow = 1),
  bottom = textGrob("Opinion difference", vjust = 0.5, gp = gpar(fontsize = 12)),
  left = textGrob("Count", rot = 90, vjust = 1, hjust = 0, gp = gpar(fontsize = 12))) +
  theme(plot.margin=unit(c(0, 0, 0.5, 0),"cm"), plot.background = element_rect(colour = "transparent", fill = "white"))


# Saving the output
ggsave(filename = "04-figures and models/Fig A2 - Histograms.png", width = 20, height = 12.5, units = "cm", dpi = 600)



# Cleanup
rm(BIBU.panel)
